using System;
using System.Data;
using System.Text;


/// <summary>
/// Summary description for cbarcode
/// </summary>
/*
Public Function Bar128(ByRef bartextin As String) As String
    Dim bartextout As String
    Dim nsum As Integer
    Dim nlen As Integer
    Dim checksumv As Integer
    Dim thisc As Byte
    Dim thiss As String
    Dim charv As Integer
    Dim startchar As String
    
    Dim i As Integer
    
    bartextout = ""
    bartextin = Trim(bartextin)
    nlen = Len(bartextin)
    nsum = 0
    startchar = "{"
    For i = 1 To nlen
        thiss = Mid(bartextin, i, 1)
        thisc = Asc(thiss)
        If thisc < 127 Then
            charv = thisc - 32
        Else
            charv = thisc - 103
        End If
        nsum = nsum + (charv * i)
        If thiss = " " Then
            bartextout = bartextout + Chr(228)
        Else
            nchar = thisc
            If (nchar = 34) Then
                bartextout = bartextout + Chr(226)
            ElseIf nchar = 123 Then
                bartextout = bartextout + Chr(194)
            ElseIf nchar = 124 Then
                bartextout = bartextout + Chr(195)
            ElseIf nchar = 125 Then
                bartextout = bartextout + Chr(196)
            ElseIf nchar = 126 Then
                bartextout = bartextout + Chr(197)
            Else
                bartextout = bartextout + thiss
            End If
        End If
        nsum = nsum Mod 103
    Next
    checksumv = nsum Mod 103
    
    If checksumv > 90 Then
        checksumc = Chr(checksumv + 103)
    ElseIf checksumv > 0 Then
        checksumc = Chr(checksumv + 32)
    Else
        checksumc = Chr(228)
    End If
    bartextout = startchar + bartextout + checksumc + "~ "
    Bar128 = bartextout
 */

public class cbarcode
{
    private const int NMAXLENGTH = 256;
    private static string strStartchar = "{";

	public cbarcode()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    /*
     * This function's used to convert a string to other string which is under barcode 128 format
     * @strInput: string which want to be converted
     * @return : string after converting
     * Maxlength of input string: 255 charactors
     * See more infromation by searching at google within keyword: "barcode"
     * */
    public static string strtobar128(string strInput)
    {
        strInput = strInput.Trim();
        ASCIIEncoding ascii = new ASCIIEncoding();
        
        byte[] commonByte = ascii.GetBytes(strInput);
        byte[] barcodeByte = new byte[NMAXLENGTH+1];
        string strBarcode = "";
        byte btCharIn, btCharOut, btSum ;
        int nsum, nlen,i;
        nlen= strInput.Length ;
        nsum = 0;
        for (i = 0; i < nlen; i++)
        {
            btCharIn = commonByte[i];
            if (btCharIn < 127)
                btCharOut = (byte)(btCharIn - 32);
            else
                btCharOut = (byte)(btCharIn - 103);
            nsum += btCharOut * (i + 1);
            if (btCharIn == 32)
                barcodeByte[i] = 228;
            else if( btCharIn==34)
                barcodeByte[i] = 226;
            else if (btCharIn == 123)
                barcodeByte[i] = 194;
            else if (btCharIn == 124)
                barcodeByte[i] = 195;
            else if (btCharIn == 125)
                barcodeByte[i] = 196;
            else if (btCharIn == 126)
                barcodeByte[i] = 197;
            else
                barcodeByte[i] = btCharOut;
            nsum = nsum % 103;
        }
        if (nsum > 90)
            btSum = (byte)(nsum + 103);
        else if (nsum > 0)
            btSum = (byte)(nsum + 32);
        else
            btSum = 228;
        barcodeByte[i] = btSum;
        strBarcode = strStartchar + ascii.GetString(barcodeByte, 0, i + 1) + "~ ";
        return strBarcode;
    }

    public static string Bar128(string bartextin) {
        string bartextout = "" ;
        int  nsum ;
        int  nlen ;
        int checksumv ;
        byte thisc ;
        char thiss ;
        int charv ;
        string startchar ;
        char checksumc;
        int i ;
        
        bartextin = bartextin.Trim();
        nlen = bartextin.Length;
        nsum = 0;
        startchar = "{";
        for( i = 0 ; i< nlen ; i++){
            thiss = bartextin[i];
            thisc = Convert.ToByte(thiss);
            if (thisc < 127) charv = thisc - 32;
            else charv = thisc - 103;
            
            nsum+=  (charv * (i+1));
            if (thiss == ' ') bartextout = bartextout + (char)228;
            else{
                byte nchar = thisc;
                if (nchar == 34) 
                    bartextout = bartextout + (char)226;
                else if (nchar == 123)
                    bartextout = bartextout + (char)194;
                else if (nchar == 124) 
                    bartextout = bartextout + (char)195;
                else if (nchar == 125) 
                    bartextout = bartextout + (char)(196);
                else if (nchar == 126) 
                    bartextout = bartextout + (char)197;
                else
                    bartextout = bartextout + thiss;
            }
            nsum = nsum % 103;
        }
        checksumv = nsum % 103;
        
        if (checksumv > 90) 
            checksumc = (char)(checksumv + 103);
        else if (checksumv > 0)
            checksumc = (char)(checksumv + 32);
        else
            checksumc = (char)(228);
        
        bartextout = startchar + bartextout + checksumc + "~ ";
        return bartextout;
    }
}
